package com.university.mentalhealthplatform.studentV3.service;

import com.university.mentalhealthplatform.appModels.Student;
import com.university.mentalhealthplatform.studentV3.dto.StudentDTOV3;
import com.university.mentalhealthplatform.studentV3.repository.StudentRepositoryV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class StudentServiceV3 {

    @Autowired
    private StudentRepositoryV3 studentRepository;

    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    public Optional<Student> findById(UUID id) {
        return studentRepository.findById(id);
    }

    public Student save(Student student) {
        return studentRepository.save(student);
    }

    public void deleteById(UUID id) {
        studentRepository.deleteById(id);
    }

    public List<Student> searchByUsername(String keyword) {
        return studentRepository.findByUsernameContaining(keyword);
    }
    // 根据用户名查找教师 DTO
    public Optional<StudentDTOV3> getStudentDTOByUsername(String username) {
        return studentRepository.findByUsername(username)
                .map(StudentDTOV3::convertToDTO);
    }

    // 根据姓名查找教师 DTO 列表
    public List<StudentDTOV3> getStudentsDTOByName(String name) {
        return studentRepository.findByNameContaining(name)
                .stream()
                .map(StudentDTOV3::convertToDTO)
                .collect(Collectors.toList());
    }


    public List<StudentDTOV3> getAllStudentsDTO() {
        return studentRepository.findAll()
                .stream()
                .map(StudentDTOV3::convertToDTO)
                .collect(Collectors.toList());
    }
}